package com.kedacom.general.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kedacom.general.param.RequestParam;
import com.kedacom.general.param.ServiceParam;
import com.kedacom.general.result.ResponseMessage;
import com.kedacom.general.service.TransmitService;
import com.kedacom.general.util.HttpUtils;
import com.kedacom.general.util.RequestBodyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@Service
public class TransmitServiceImpl implements TransmitService {
    @Value("${transmit.url:http://10.67.7.21:8083/transmit/afterTransmit}")
    private String transmitUrl;
    @Override
    public ResponseMessage transmit(HttpServletRequest request) {
        // 获取请求参数体
        try {
            RequestParam requestParam = RequestBodyUtil.getRequestParam(request);
            if (requestParam!=null){
                JSONObject parameter = (JSONObject) requestParam.getParam().get("parameter");
                ServiceParam.ParameterBean parameterBean = parameter.toJavaObject(ServiceParam.ParameterBean.class);
                String value = parameterBean.getCondition().getKeyValueList().get(0).getValue();
                log.info("********************************调用后置服务地址"+ transmitUrl+",参数"+value);
                ResponseMessage httpPostResult = HttpUtils.getHttpPostResult(transmitUrl, value, null);
                return httpPostResult;
            }
            log.error("接口参数为空");
            return new ResponseMessage("系统异常",null,500,"-1");
        } catch (IOException e) {
            log.error("读取url接口参数异常");
            return new ResponseMessage("系统异常",null,500,"-1");
        }
    }
}
